package cn.stylefeng.guns.czlm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.czlm.entity.TbProduct;
import cn.stylefeng.guns.czlm.service.TbProductService;
import cn.stylefeng.guns.czlm.warpper.productWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/Product")
public class productController  extends BaseController {
    TbProduct tbProduct=   new TbProduct();

    @Autowired
    private TbProductService tbProductService;
    private static String PREFIX = "/modular/system/product/";

    @RequestMapping("/index")//主页
    public String s() {
     return PREFIX +"product.html";
    }

   static String topId;
    @RequestMapping("/ZparameterValue")
    public String byZparameterValue(String id) {//在页面里打开  parameterValue页面
       topId=id;
        return PREFIX +"parameterValue.html";
    }
    @RequestMapping("/parameterValue")////在页面拿到id 查询 parameterValue进入参数页面
    @ResponseBody
    public TbProduct byId() {
        System.err.println(topId);
        if(topId==null){
            return null;
        }
        Integer Pid = Integer.parseInt(topId);
        return  tbProductService.tbProductById(Pid);
    }
    @RequestMapping("/parameterValueModifySave")
    @ResponseBody
    public String parameterValueModifySave(@RequestParam String res) {//在页面里打开  parameterValue的返回值json字符串
        System.out.println(res);
       String str= res.replace("][" , ",");
        System.err.println(str);
      return str;
    }

   @RequestMapping("/ModifySave")
    public String ModifySave(TbProduct tbProduct) {
       tbProductService.ModifySave(tbProduct);
       return PREFIX +"product.html";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object ProduceMap(TbProduct tbProduct){//查询Product表
        System.err.println(tbProduct.toString()+"老四");
        Page<Map<String, Object>> slist = tbProductService.listProductByMap(tbProduct);
        //Wrapper数据传到页面上需要进行一次封装
        Page<Map<String, Object>> warpper = new productWrapper(slist).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    /***
     * 跳转到 新增页面
     * @return
     */
    @RequestMapping("/TjPageAddProduct")
    public String TjPageAdd(){
        return PREFIX+"productAdd.html";
    }

    @RequestMapping("/AddProductSave")
    public String productAdd(TbProduct tbProduct){
        tbProduct.setId(4444);
        System.err.println(tbProduct.getId());
        tbProductService.insertTbProduct(tbProduct);
        return PREFIX +"product.html";
    }

    @RequestMapping("/del")
    public @ResponseBody
        String Del(Integer id,Boolean isMarketable) {
        if(null!=isMarketable && !isMarketable){
          return    tbProductService.delProduct(id)+"";
        }
         return "非下架物品不能删除!";
    }

    /**
     * 修改上下架
     * @param id
     * @param isMarketable
     * @return
     */
    @RequestMapping("/product_soldout")
    public  @ResponseBody
    String soldTop(String id,Boolean isMarketable) {

   try {
       tbProduct.setId(Integer.valueOf(id));
   }catch (Exception e){
       return "傻掉吧你!";
   }
        if(null!=isMarketable &&!isMarketable){//为 假 ==0  修改为1
            tbProduct.setIsMarketable(1);
          return   tbProductService.ModifyIsMarketable(tbProduct)+"";

        }else if(null!=isMarketable &&isMarketable){//为真 == 1 修改为 0
            tbProduct.setIsMarketable(0);
           return tbProductService.ModifyIsMarketable(tbProduct)+"";
        }
        return "只有上架物品才能下架!";
    }


    @RequestMapping("/unfreeze")//揭开推荐
    public String unfreeze(Integer id) {
        tbProduct.setId(id);
        tbProduct.setIsRecommend(1);
        tbProductService.ModifyIsRecommend(tbProduct);
        return "0";


    }
    @RequestMapping("/freeze")//取消推荐
    public String Freeze(Integer id) {
        tbProduct.setId(id);
        tbProduct.setIsRecommend(0);
        tbProductService.ModifyIsRecommend(tbProduct);
      return   "1";
    }

    @RequestMapping("/SkipModify{id}")//查询的跳转
    public  String SkipModify(@RequestParam Integer id) {
        LogObjectHolder.me().set(id);
        return PREFIX+"ModifyProduct.html";
    }
    @RequestMapping("/SkipModifySave{id}")//修改时 id查询
    @ResponseBody
    public  Object SkipModifySave(@RequestParam Integer id,HttpSession session) {
        TbProduct tbProduct=  tbProductService.tbProductById(id);
              Map<String, Object> map = BeanUtil.beanToMap(tbProduct);
        return ResponseData.success(map);
    }

    /***
     * 跳转页面img
     * @return
     */
    @RequestMapping("/img")//点击图片的弹框
    public String img() {
        return PREFIX+"img.html";
    }

}
